<?php
/*
 * Auther Bachue
 * Function Library for List, Add and Delete A SVN Repository
 */

include_once './config/config.php';

/*
 * function listRepository()
 * Auther Bachue
 * Description: List Repository in $config[SvnParentPath]
 * Return:an Array includes All Svn Repositories name
 */

function listRepository()
{
    global $config;
    $resultString=preg_replace("/\\n$/",'',    //To clear the last newline '\n'
                                shell_exec("{$config['LsPath']} '{$config['SvnParentPath']}' "));
                                //e.g. ls /var/www/svn
    if(empty($resultString))
    {
        return array();
    }
    return explode("\n",$resultString); //Explode Array by newline '\n'
}

/*
 * function addNewRepository($repositoryName,$repositoryOwner)
 * Auther Bachue
 * Description: Create a new Svn Repository called $repositoryName and ownerd by $repositoryOwner
 * Return:any Error infomation from shell
 */
function addNewRepository($repositoryName,$repositoryOwner)
{
    $repositoryName=trim($repositoryName);
    $repositoryOwner=trim($repositoryOwner);
    if(empty($repositoryName) || empty($repositoryOwner)) return 'repository name and owner can\'t be empty';

    if(!preg_match(VALID_REGEX, $repositoryName) ||
       !preg_match(VALID_REGEX, $repositoryOwner))
    {
        return 'repository name and owner name can\'t contain any illegal character';
    }
    global $config;
    
    //Why do this? Because we wanna to keep Svn Parent Dir NOT empty. If it is empty, we can create
    //a svn repository in the Parent Dir directly which we must forbid. But if is not empty completely,
    //svnadmin will refuse create a repository in it. So we will touch a random file in it.
    $info=shell_exec("{$config['TouchPath']} '{$config['SvnParentPath']}/null' 2>&1 1> /dev/null;".
                    SET_UTF8_ZH_CN_ENCODING."{$config['SvnAdminPath']} create '{$config['SvnParentPath']}/{$repositoryName}' 2>&1 1> /dev/null;".
                    //e.g. /usr/bin/svnadmin create /var/www/svn/repos
                    "rm -f '{$config['SvnParentPath']}/null' 2>&1 1> /dev/null;".
                    //delete null file
                    "{$config['SvnPath']} import '{$config['SvnInitRepositoryPath']}' 'file://{$config['SvnParentPath']}/{$repositoryName}/' -m 'Init' --username {$repositoryOwner} 2>&1 1> /dev/null");
                    //e.g. /usr/bin/svn import /etc/httpd/conf.d/init/ file:///var/www/svn/repos/repo1 \
                    // -m "Init" --username svnuser
                    // To copy some inital file/folder, e.g. branches/ tags/ trunks/, to new repository
                    // change their owner to what you given.
    if(!empty($info))   //if sth output, that must be wrong, because STDOUT redirected to /dev/null
    {
        //TODO do log here!
        return $info;
    }
}

/*
 * function deleteRepository($repositoryName)
 * Auther Bachue
 * Description: Delete An existed Repository whose name You given
 * Return:any Error infomation from shell
 */
function deleteRepository($repositoryName)
{
    $repositoryName=trim($repositoryName);
    //can not run likes rm -rf /var/www/svn/ , its very danger!
    if(empty($repositoryName)) return 'repository name can\'t be empty';
    //can not run likes rm -rf /var/www/svn/* , its very danger!
    if(strpos($repositoryName, '*')!==false) return 'star can not appear in repository name';
    if(!preg_match(VALID_REGEX, $repositoryName))
    {
        return 'repository name can\'t contain any illegal character';
    }
    global $config;

    //We must protect you from delete other dir which are not Svn Repository Dir
    //so we get the Dir path and Svn Parent Path, Dir path should be the subdir of Svn Parent Path
    //if Dir path is not existed or not subdir, return wrong!
    $info=shell_exec("cd '{$config['SvnParentPath']}/{$repositoryName}';pwd -P;");
    if(empty ($info))
    {
        //TODO do log here!
        return 'File System Error';
    }
    if(strpos($info,$config['SvnParentPath'])!==0 || $info===shell_exec("cd '{$config['SvnParentPath']}';pwd -P"))
    {
        //TODO do log here!
        return 'File System Error';
    }

    $info=shell_exec("{$config['RmPath']} -rf '{$config['SvnParentPath']}/{$repositoryName}' 2>&1 1> /dev/null");
    //e.g. /bin/rm -rf /var/www/svn/repos/repo1
    // delete the repository folder
    if(!empty($info))    //if sth output, that must be wrong, because STDOUT redirected to /dev/null
    {
        //TODO do log here!
        return $info;
    }
}

?>
